<?php

namespace app\api\controller;

use app\api\basic\BaseController;
use support\Request;
use app\activity\logic\activityLogic;
use app\signup\logic\signupLogic;
use app\api\annotation\LoginRequired;

class ActivityController extends BaseController {
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
    #[LoginRequired(required:false)]
    public function activityList(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
            ['keyword',''],
            ['limit',10],
            ['page',1],
            ['activity_category_id','']
           
        ]);
       // var_dump($param);
        $activity=new  activityLogic();
        if($param['keyword']){
            $arr =[
                'lang_code'=>$param['lang_code'],
            ];
        }else{
            $arr =[
                'lang_code'=>$param['lang_code'],
                'activity_category_id'=>$param['activity_category_id']
            ];
        }
        $query=$activity->search($arr);
        if($param['keyword']){
            $query->where('activity_title|activity_details', 'like', "%{$param['keyword']}%");
        }
        // $where['lang_code']=$param['lang_code'];



        $signup=new  signupLogic();
        $activityData=$query
         ->field('activity_id,activity_title,activity_cover,create_time,activity_start_time,activity_end_time')
        ->where('is_display',1)
        ->whereNull('delete_time')
        ->order('status asc')
//        ->order('sort_order desc')
//        ->order('create_time desc')
        ->paginate($param['limit']);
        $data=$activityData->items();
        //var_dump($data);
        foreach($data as &$value){
         $value->is_use=0;
         $signupData=$signup->search(['activity_id'=>$value->activity_id,'user_id'=>$this->userId])->findOrEmpty();
         if(!$signupData->isEmpty()) $value->is_use=1;
        }
        return $this->success(['data'=>$data,'total'=>$activityData->total()]);
    }
    /**
     * 活动详情
     */
    #[LoginRequired(required:false)]
    public function activityDetail(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
            ['activity_id',''],
        ]);
        $activity=new  activityLogic();
        $signup=new  signupLogic();
        if(!$param['activity_id']) return $this->fail('缺少活动id');
        $activityData=$activity->search(['activity_id'=>$param['activity_id']])
         ->field('activity_category_id,activity_id,activity_title,activity_cover,create_time,activity_start_time,activity_end_time,activity_details')
        ->where('is_display',1)
        ->whereNull('delete_time')
        ->order('sort_order desc')
        ->order('create_time desc')
        ->find();
       
        if($activityData){
            $signupData=$signup->search(['activity_id'=>$param['activity_id'],'user_id'=>$this->userId])->findOrEmpty();
            if(!$signupData->isEmpty()) $activityData->is_use=1;
             return $this->success($activityData->toArray());
        }
        return $this->fail('数据不存在');
    }
    public function signup(Request $request){
           $param = $request->more([
            ['activity_id',''],
            ['member_name',''],
            ['sex',''],
            ['phone_number',''],
            ['company_name',''],
            ['company_position',''],
            // ['signup_time','']
        ]);
         $validate = $this->validate->rule([
        'activity_id'    => 'require|number',
        'member_name'    => 'require|max:50',
        'sex'            => 'require|in:1,2,3',
        'phone_number'   => 'require|regex:/^1[3-9]\d{9}$/',
        'company_name'   => 'require|max:100',
        'company_position'=> 'require|max:50',
        // 'signup_time'    => 'require|dateFormat:Y-m-d H:i:s'
    ])->message([
        'activity_id.require' => '活动ID不能为空',
        'phone_number.regex'  => '手机号格式不正确',
        // 'signup_time.dateFormat' => '报名时间格式应为 Y-m-d H:i:s'
    ]);

    if (!$validate->check($param)) {
        return $this->fail($validate->getError());
    }
      
          $activity=new  activityLogic();
        $signup=new  signupLogic();
         if(!$param['activity_id']) return $this->fail('缺少活动id');
        $activityData=$activity->search(['activity_id'=>$param['activity_id']])
         ->field('activity_category_id,activity_id,activity_title,activity_cover,create_time,activity_start_time,activity_end_time,activity_details')
        ->where('is_display',1)
        ->whereNull('delete_time')
        ->order('sort_order desc')
        ->order('create_time desc')
        ->findOrEmpty();
        if($activityData->isEmpty()) return $this->fail('活动不存在');
        $param['signup_time']=date('Y-m-d H:i:s');
        $signupData=$signup->search(['activity_id'=>$param['activity_id'],'user_id'=>$this->userId])->findOrEmpty();
        if(!$signupData->isEmpty()) return $this->fail('请勿重复报名');
        $param['user_id']=$this->userId;
         $res=$signup->create($param);
         if($res) return $this->success('报名成功');
         return $this->fail('报名失败');
    }
}